package com.song.invoicing.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.song.invoicing.exception.PurchaseRecordException;
import com.song.invoicing.mapper.ProductMapper;
import com.song.invoicing.mapper.PurchaseRecordMapper;
import com.song.invoicing.mapper.UserMapper;
import com.song.invoicing.pojo.dao.Product;
import com.song.invoicing.pojo.dao.PurchaseRecord;
import com.song.invoicing.pojo.dto.PurchaseQueryDTO;
import com.song.invoicing.pojo.dto.PurchaseRecordUpdateStatusDTO;
import com.song.invoicing.service.PurchaseRecordService;
import com.song.invoicing.utils.BaseContext;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class PurchaseRecordServiceImpl implements PurchaseRecordService {
    private final PurchaseRecordMapper purchaseRecordMapper;
    private final UserMapper userMapper;
    private final ProductMapper productMapper;
    @Override
    public Map<String, Object> getPurchaseRecords(PurchaseQueryDTO purchaseQueryDTO) {
        PageHelper.startPage(purchaseQueryDTO.getPage(),purchaseQueryDTO.getPageSize());
        Page<PurchaseRecord> page = purchaseRecordMapper.getPurchaseRecords(purchaseQueryDTO);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("total",page.getTotal());
        resultMap.put("list",page.getResult());
        return resultMap;
    }

    @Transactional
    public void updateStatus(PurchaseRecordUpdateStatusDTO purchaseRecordUpdateStatusDTO) {
        //1.获取用户id，来判断当前用户是否属于管理员
        Long userId = BaseContext.getCurrentId();
        byte role = userMapper.getUserRole(userId);
        if(role!=0){
            //非管理员，驳回，报错
            throw new PurchaseRecordException("非管理员，无法操作");
        }
            purchaseRecordMapper.updateStatus(purchaseRecordUpdateStatusDTO);
            if(purchaseRecordUpdateStatusDTO.getPurchaseStatus()!=2){
                return;
            }
           Product newProduct = purchaseRecordMapper.getPurchaseRecord(purchaseRecordUpdateStatusDTO.getPurchaseId());
           Product currentProduct =  productMapper.selectProductByProductName(newProduct.getProductName());
           if(currentProduct==null){
               //当前商品还不存在，进行创建
               productMapper.addNewProduct(newProduct);
           }else{
               //商品已经存在，进行增加商品库存
               productMapper.addProductStock(currentProduct.getProductId(),newProduct.getStock());
           }
    }

    @Override
    public void addPurchaseRecord(PurchaseRecord purchaseRecord) {
            purchaseRecord.setPurchaseAmount(purchaseRecord.getUnitPrice()*purchaseRecord.getPurchaseQuantity());
            purchaseRecordMapper.addPurchaseRecord(purchaseRecord);
    }

    @Override
    public void updatePurchaseRecord(PurchaseRecord purchaseRecord) {
        purchaseRecord.setPurchaseAmount(purchaseRecord.getUnitPrice()*purchaseRecord.getPurchaseQuantity());
        purchaseRecordMapper.updatePurchaseRecord(purchaseRecord);
    }
}
